/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests;

import Utilities.PermissionsConstants;

/**
 * Instances of this class are used to pass server management requests from the
 * user interface to the server management service(s).
 *
 * @author Angel Sanadinov
 */
public class ServerManagementRequest extends RequestBean
{
    private String name;                //the name of the server
    private String description;         //the description of the server
    private String webServiceAddress;   //the web service address associated with the server
    private String webServiceUser;      //the web serivce username, used at logon
    private String webServicePassword;  //the web service password, used at logon
    private String sharedFoldersRoot;   //the shared folders root directory of the server
    private int vrdpPortsRangeLow;      //range of available VRDP ports: low limit
    private int vrdpPortsRangeHigh;     //range of available VRDP ports: high limit
    private int vrdpPortsPerMachine;    //number of ports per virtual machine

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     */
    public ServerManagementRequest()
    {
        this.name = null;
        this.description = null;
        this.webServiceAddress = null;
        this.webServiceUser = null;
        this.webServicePassword = null;
        this.sharedFoldersRoot = null;
        this.vrdpPortsRangeLow = 0;
        this.vrdpPortsRangeHigh = 0;
        this.vrdpPortsPerMachine = 0;
    }

    /**
     * Constructs the request using the supplied data for server creation.
     *
     * @param requestorId the id of the user that requested the action
     * @param serverName the name of the new server
     * @param serverDescription the description of the new server
     * @param webServiceAddress the server's web service URL
     * @param webServiceUsername the server's web service username
     * @param webServicePassword the server's web service password
     * @param foldersRoot the server's shared folders root directory
     * @param vrdpPortsRangeLow lower boundary of the VRDP ports range for the server
     * @param vrdpPortsRangeHigh higher boundary of the VRDP ports range for the server
     * @param vrdpPortsPerMachine number of ports for each virtual machine
     */
    public ServerManagementRequest(int requestorId, String serverName, String serverDescription,
                                   String webServiceAddress, String webServiceUsername, 
                                   String webServicePassword, String foldersRoot,
                                   int vrdpPortsRangeLow, int vrdpPortsRangeHigh, int vrdpPortsPerMachine)
    {
        super.setRequestorId(requestorId);
        this.name = serverName;
        this.description = serverDescription;
        this.webServiceAddress = webServiceAddress;
        this.webServiceUser = webServiceUsername;
        this.webServicePassword = webServicePassword;
        this.sharedFoldersRoot = foldersRoot;
        this.vrdpPortsRangeLow = vrdpPortsRangeLow;
        this.vrdpPortsRangeHigh = vrdpPortsRangeHigh;
        this.vrdpPortsPerMachine = vrdpPortsPerMachine;
        super.setRequestedAction(PermissionsConstants.ACTION_SERVER_CREATE);
    }

    /**
     * Constructs the request using the supplied data for server modification.
     *
     * @param requestorId the id of the user that requested the action
     * @param serverId the id of the server to be modified
     * @param serverName the new name of the server
     * @param serverDescription the new description of the server
     * @param webServiceAddress the new web service URL
     * @param webServiceUsername the new web service username
     * @param webServicePassword the new web service password
     * @param foldersRoot the new shared folders root directory
     * @param vrdpPortsRangeLow lower boundary of the VRDP ports range for the server
     * @param vrdpPortsRangeHigh higher boundary of the VRDP ports range for the server
     * @param vrdpPortsPerMachine number of ports for each virtual machine
     */
    public ServerManagementRequest(int requestorId, int serverId, String serverName, 
                                   String serverDescription, String webServiceAddress,
                                   String webServiceUsername, String webServicePassword,
                                   String foldersRoot, int vrdpPortsRangeLow,
                                   int vrdpPortsRangeHigh, int vrdpPortsPerMachine)
    {
        this(requestorId, serverName, serverDescription, webServiceAddress,
             webServiceUsername, webServicePassword, foldersRoot,
             vrdpPortsRangeLow, vrdpPortsRangeHigh, vrdpPortsPerMachine);
        super.setRequestedAction(PermissionsConstants.ACTION_SERVER_MODIFY);
        super.setServerId(serverId);
    }

    /**
     * Constructs the request using the supplied data for server removal.
     *
     * @param requestorId the id of the user that requested the action
     * @param serverId the id of the server to be removed
     */
    public ServerManagementRequest(int requestorId, int serverId)
    {
        super.setRequestorId(requestorId);
        super.setServerId(serverId);
        super.setRequestedAction(PermissionsConstants.ACTION_SERVER_REMOVE);
    }

    /**
     * Retrieves the name of the server, if available.
     *
     * @return the name of the server
     */
    public String getName()
    {
        return name;
    }

    /**
     * Retrieves the server's description, if available.
     *
     * @return the server's description
     */
    public String getDescription()
    {
        return description;
    }

    /**
     * Retrieves the server's web service URL, if available.
     *
     * @return the web service URL
     */
    public String getWebServiceAddress()
    {
        return webServiceAddress;
    }

    /**
     * Retrieves the server's web service username, if available.
     *
     * @return the web service username
     */
    public String getWebServiceUser()
    {
        return webServiceUser;
    }

    /**
     * Retrieves the server's web service password, if available.
     *
     * @return the web service password
     */
    public String getWebServicePassword()
    {
        return webServicePassword;
    }

    /**
     * Retrieves the server's shared folders root directory.
     *
     * @return the shared folders root directory
     */
    public String getSharedFoldersRoot()
    {
        return sharedFoldersRoot;
    }

    /**
     * Retrieves the lower boundary of the VRDP ports range for the server.
     *
     * @return the lower range boundary
     */
    public int getVrdpPortsRangeLow()
    {
        return vrdpPortsRangeLow;
    }

    /**
     * Retrieves the higher boundary of the VRDP ports range for the server.
     *
     * @return the higher range boundary
     */
    public int getVrdpPortsRangeHigh()
    {
        return vrdpPortsRangeHigh;
    }

    /**
     * Retrieves the number of ports each virtual machine is allocated.
     *
     * @return the number of ports per machine (number of ports in a slot)
     */
    public int getVrdpPortsPerMachine()
    {
        return vrdpPortsPerMachine;
    }

    /**
     * Sets the server's name.
     *
     * @param name the name of the server
     */
    public void setName(String name)
    {
        this.name = name;
    }
    
    /**
     * Sets the server's description.
     *
     * @param description the server description
     */
    public void setDescription(String description)
    {
        this.description = description;
    }

    /**
     * Sets the server's web service URL.
     *
     * @param webServiceAddress the web service URL
     */
    public void setWebServiceAddress(String webServiceAddress)
    {
        this.webServiceAddress = webServiceAddress;
    }

    /**
     * Sets the server's web service username.
     *
     * @param webServiceUser the web service username
     */
    public void setWebServiceUser(String webServiceUser)
    {
        this.webServiceUser = webServiceUser;
    }

    /**
     * Sets the server's web service password.
     *
     * @param webServicePassword the web service password
     */
    public void setWebServicePassword(String webServicePassword)
    {
        this.webServicePassword = webServicePassword;
    }

    /**
     * Sets the server's shared folders root directory.
     *
     * @param foldersRoot the shared folders root directory
     */
    public void setSharedFoldersRoot(String foldersRoot)
    {
        this.sharedFoldersRoot = foldersRoot;
    }

    /**
     * Sets the server's VRDP ports range lower boundary.
     *
     * @param vrdpPortsRangeLow the lower boundary
     */
    public void setVrdpPortsRangeLow(int vrdpPortsRangeLow)
    {
        this.vrdpPortsRangeLow = vrdpPortsRangeLow;
    }

    /**
     * Sets the server's VRDP ports range higher boundary.
     *
     * @param vrdpPortsRangeHigh the higher boundary
     */
    public void setVrdpPortsRangeHigh(int vrdpPortsRangeHigh)
    {
        this.vrdpPortsRangeHigh = vrdpPortsRangeHigh;
    }

    /**
     * Sets the server's number of ports for each virtual machine.
     *
     * @param vrdpPortsPerMachine the number of ports per machine
     */
    public void setVrdpPortsPerMachine(int vrdpPortsPerMachine)
    {
        this.vrdpPortsPerMachine = vrdpPortsPerMachine;
    }

    /**
     * Checks the validity of the object. <br><br>
     *
     * This object is valid only when all required data for the requested action is
     * valid.
     *
     * @return <code>true</code> if the object is valid or <code>false</code> otherwise
     */
    @Override
    public boolean isValid()
    {
        boolean result = false;

        //checks if the requestor is valid
        if(!isRequestorIdValid())
            return result;
        else
            ;

        //based on the action that is requested, different validity checks are performed
        switch(getRequestedAction())
        {
            //at least a server name and a service URL are needed
            case PermissionsConstants.ACTION_SERVER_CREATE:
            {
                if(name != null && !name.trim().equals("") && webServiceAddress != null
                        && !webServiceAddress.trim().equals("") && sharedFoldersRoot != null
                        && !sharedFoldersRoot.trim().equals("") && vrdpPortsRangeLow > 0
                        && vrdpPortsRangeLow < vrdpPortsRangeHigh)
                    result = true;
                else
                    result = false;
            } break;

            //at least a server id, name and service URL are needed
            case PermissionsConstants.ACTION_SERVER_MODIFY:
            {
                if(isServerIdValid() && name != null && !name.trim().equals("")
                        && webServiceAddress != null && !webServiceAddress.trim().equals("")
                        && sharedFoldersRoot != null && !sharedFoldersRoot.trim().equals("")
                        && vrdpPortsRangeLow > 0 && vrdpPortsRangeLow < vrdpPortsRangeHigh)
                    result = true;
                else
                    result = false;
            } break;

            //at least a server id is needed
            case PermissionsConstants.ACTION_SERVER_REMOVE:
            {
                if(isServerIdValid())
                    result = true;
                else
                    result = false;
            } break;

            default: result = false;
        }

        return result;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (name,description,serviceAddress,sharedFoldersRoot,
     * vrdpPortsRangeLow,vrdpPortsRangeHigh,vrdpPortsPerMachine)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        return ServerManagementRequest.class.getSimpleName()
                + ": (" + name + "," + description + "," + webServiceAddress 
                + "," + sharedFoldersRoot + "," + vrdpPortsRangeLow + ","
                + vrdpPortsRangeHigh + "," + vrdpPortsPerMachine + ")";
    }
}
